package com.kstudio.videocapture.util;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.view.LayoutInflater;
import android.view.ViewGroup.LayoutParams;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kstudio.videocapture.R;

public class KhProgressDialog {
    private static final int MODE_STYLE_LARGE       = 0x0001;
    private static final int MODE_STYLE_HORIZONTAL  = 0x0002;

    private int         mMode;
    private Dialog      mDialog;
    private TextView    mTitle;

    private KhProgressDialog(int mode, Dialog dialog, TextView title) {
        mMode = mode;
        mDialog = dialog;
        mTitle = title;
    }

    public static KhProgressDialog showStyleLarge(Context context) {
        Dialog dialog = new Dialog(context, R.style.KhProgressDialog);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ProgressBar view = (ProgressBar) inflater.inflate(R.layout.progressbar_style_large, null);

        dialog.addContentView(view, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        dialog.setCancelable(false);
        dialog.show();

        return new KhProgressDialog(MODE_STYLE_LARGE, dialog, null);
    }

    public static KhProgressDialog showStyleHorizontal(Context context, CharSequence title, int max, OnCancelListener cancelListener) {
        ProgressDialog dialog = new ProgressDialog(context, R.style.KhProgressDialog);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.progressbar_style_horizontal_title, null);

        TextView titleView = (TextView) view.findViewById(R.id.progressbar_style_horizontal_title);
        titleView.setText(title);

        dialog.setCustomTitle(view);
        dialog.setIndeterminate(false);
        dialog.setMax(max);
        dialog.setSecondaryProgress(1);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setOnCancelListener(cancelListener);
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getResources().getText(R.string.Cancel), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }

        });
        dialog.show();

        return new KhProgressDialog(MODE_STYLE_HORIZONTAL, dialog, titleView);
    }

    public void setProgress(int value) {
        if(mMode == MODE_STYLE_HORIZONTAL) {
            ((ProgressDialog) mDialog).setProgress(value);
            ((ProgressDialog) mDialog).setSecondaryProgress(value + 1);
        }
    }

    public void setTitle(CharSequence title) {
        if(mMode == MODE_STYLE_HORIZONTAL) {
            if(mTitle != null) mTitle.setText(title);
        }
    }

    public void dismiss() {
        mDialog.dismiss();
    }
}